package com.aidong.jobs;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.aidong.jobs.constants.JobConst;
import com.aidong.login.biz.MemberIntegralBiz;
import com.aidong.login.biz.MemberIntegralLogBiz;
import com.aidong.login.entity.MemberIntegralLog;
import com.aidong.pay.biz.OrderManagementBiz;

@Component("integralJob")
public class IntegralJob {
	@Autowired
	private MemberIntegralBiz integralBiz;
	@Autowired
	private MemberIntegralLogBiz logBiz;
	@Autowired
	private OrderManagementBiz orderBiz;
	

	//@Scheduled(cron = "0 0 3 0/1 * ?")
	public void integralJob() {
		List<Integer> ids = new ArrayList<Integer>();
		List<MemberIntegralLog> logs = logBiz.getUnconfirmIntegral();
		for (MemberIntegralLog log : logs) {
			integralBiz.increase(log.getIdongId(), log.getAmount());
			ids.add(log.getId());
		}
		logBiz.confirmIntegrals(ids);
	}
	//@Scheduled(cron = "0 0/1 * * * ?")
	public void orderMessageJob() {
		orderBiz.getOrderMessage();
	}
	//@Scheduled(cron = "0 0/1 * * * ?")
	public void checkCodeJob() {
		ConcurrentMap<String, String[]> codeMap=JobConst.codeMap;
		String[] code=null;
		
		long time=System.currentTimeMillis();
		for(String key:codeMap.keySet()){
			code=codeMap.get(key);
			
			long times=Long.parseLong(code[1]);
			long second=((time-times)/1000);
			if(second>=300){
				JobConst.codeMap.remove(key);
			}
		}
	}
	
	//@Scheduled(cron = "0 0/1 * * * ?")
	public void checkLuckyDrawJob() {
		ConcurrentMap<String, Long> codeMap=JobConst.activeMap;
		long time=System.currentTimeMillis();
		for(String key:codeMap.keySet()){
			long times=codeMap.get(key);
			long second=((time-times)/1000);
			if(second>=60){
				JobConst.activeMap.remove(key);
			}
		}
	}
	
	//@Scheduled(cron = "0 0/1 * * * ?")
	public void checkMsgCodeJob() {
		ConcurrentMap<String, String[]> codeMap=JobConst.msgCodeMap;
		String[] code=null;
		
		long time=System.currentTimeMillis();
		for(String key:codeMap.keySet()){
			code=codeMap.get(key);
			long times=Long.parseLong(code[1]);
			long second=((time-times)/1000);
			if(second>=1800){
				JobConst.msgCodeMap.remove(key);
			}
		}
	}
}
